package com.zaoniao.enstudy.api.controller;

import com.zaoniao.enstudy.api.vo.ZaoWordExampleVo;
import com.zaoniao.enstudy.api.vo.ZaoWordVo;
import com.zaoniao.enstudy.domain.entity.ZaoWord;
import com.zaoniao.enstudy.domain.entity.ZaoWordExample;
import com.zaoniao.enstudy.domain.service.ZaoWordExampleService;
import com.zaoniao.enstudy.domain.service.ZaoWordService;
import com.zaoniao.enstudy.infracore.constants.AppConstant;
import com.zaoniao.enstudy.infracore.enums.GradeEnums;
import com.zaoniao.enstudy.infracore.web.DataResponse;
import com.zaoniao.enstudy.infracore.web.PagedGridResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 单词api视图层.
 *
 * @author llb
 */
@RestController
@Tag(name = "单词相关api")
@RequestMapping("/words")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class WordController {

    private final ZaoWordService zaoWordService;
    private final ZaoWordExampleService zaoWordExampleService;

    /**
     * 年级列表查询.（前端写死，该接口未使用）
     *
     * @return 年级列表信息
     */
    @Deprecated
    @Operation(summary = "查询年级列表")
    @GetMapping("/grades")
    public DataResponse<Map<String, Object>> grade() {
        return DataResponse.ok(GradeEnums.getAllGradeValues());
    }

    /**
     * 根据年级查询年级内10条随机单词.
     *
     * @param grade  年级
     * @param userId 用户Id (收藏标签展示需要，显示是否为当前登录人收藏，非必填)
     * @param page   第几页
     * @param size   每页显示数量
     * @return 单词列表信息
     */
    @Operation(summary = "根据年级查询年级内10条随机单词，单点登录尚未实现，用户id直传测试")
    @GetMapping("/grade/{grade}")
    public DataResponse<PagedGridResult<ZaoWordVo>> wordsLimit(
            @Parameter(name = "grade", description = "年级") @PathVariable Integer grade,
            @Parameter(name = "userId", description = "用户", allowEmptyValue = true) @RequestParam(value = "userId", required = false) String userId,
            @Parameter(name = "page", description = "第几页", allowEmptyValue = true) @RequestParam(value = "page", required = false) Integer page,
            @Parameter(name = "size", description = "每一页显示的条数", allowEmptyValue = true) @RequestParam(value = "size", required = false) Integer size) {
        if (page == null) {
            page = AppConstant.PAGE;
        }
        if (size == null) {
            size = AppConstant.SIZE;
        }
//        String userId = "o1D2W4rYToA9IWYzT5hC3ojFf0II";
        PagedGridResult<ZaoWord> pagedGridResult = zaoWordService.getWordListRand(grade, userId, page, size);
        return DataResponse.ok(ZaoWordVo.Converter.INSTANCE.do2voPage(pagedGridResult));
    }

    /**
     * 单词详情查询.
     *
     * @param word 单词
     * @return 单词详情信息
     */
    @Operation(summary = "根据单词查询单词详情以及其相关联的例句")
    @GetMapping("/{word}")
    public DataResponse<ZaoWordVo> detail(@Parameter(name = "word", description = "单词") @PathVariable String word) {
        ZaoWord zaoWord = zaoWordService.getWordDetail(word);
        return DataResponse.ok(ZaoWordVo.Converter.INSTANCE.do2Vo(zaoWord));
    }

    @Deprecated
    @Operation(summary = "根据单词查询其相关联的例句.")
    @GetMapping("/example/{word}")
    public DataResponse<List<ZaoWordExampleVo>> wordsLimit(@PathVariable String word) {
        List<ZaoWordExample> list = zaoWordExampleService.getExamples(word);
        return DataResponse.ok(ZaoWordExampleVo.Converter.INSTANCE.do2VoList(list));
    }

}
